package cn.zebra.dev.admin.module.order.controller;


import cn.zebra.dev.admin.module.login.SecurityAdminService;
import cn.zebra.dev.admin.module.order.vo.OrdersExpressVO;
import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.order.module.express.entity.OrdersExpressEntity;
import cn.zebra.dev.order.module.express.service.OrdersExpressService;
import cn.zebra.dev.order.module.order.constant.OrdersStatusConstant;
import cn.zebra.dev.order.module.order.entity.OrdersEntity;
import cn.zebra.dev.order.module.order.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;


@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersExpressService ordersExpressService;

    @Autowired
    private SecurityAdminService securityAdminService;


    @ApiOperation("订单确认发货")
    @PostMapping("/delivery")
    @Transactional(rollbackFor = Exception.class)
    public Result orderDelivery(@Valid @RequestBody List<OrdersExpressVO> vo){
        int lastTime = 0;
        for (OrdersExpressVO ordersExpressVO : vo) {
            OrdersEntity ordersEntity = ordersService.findByOrderNo(ordersExpressVO.getOrdersSn());
            if (ordersEntity == null){
                return Result.error("订单号不存在，请仔细确认后重新输入!");
            }
            if (!ordersEntity.getOrdersStatus().equals(OrdersStatusConstant.WAIT_SHIPPED)){
                return Result.error("您的订单不是待发货状态！");
            }
            OrdersExpressEntity ordersExpressEntity = new OrdersExpressEntity();
            ordersExpressEntity.setOrdersId(ordersEntity.getId());
            OrdersExpressEntity ordersExpressEntity1 =ordersExpressService
                    .findExpressSn(ordersExpressVO.getExpressSn(),securityAdminService.getId());
            if (ordersExpressEntity1 != null){
                return Result.success("您输入的快递单号有重复信息，请仔细检查重新输入!");
            }
            ordersExpressEntity.setExpressSn(ordersExpressVO.getExpressSn());
            ordersExpressEntity.setExpressCode(ordersExpressVO.getExpressCode());
            ordersExpressEntity.setExpressTime(LocalDateTime.now());
            ordersExpressEntity.setExpressUserId(securityAdminService.getId());
            if (!ordersExpressService.save(ordersExpressEntity)){
                return Result.error("发货失败");
            }
            lastTime ++;
            //如果是最后一次循环就设置订单状态为已发货
            if (lastTime == vo.size()){
                if(!ordersService.updateOrderStatusById(ordersEntity.getId(),OrdersStatusConstant.SHIPPED)){
                    return Result.error("发货失败");
                }
            }
        }
        return Result.success("发货成功!");
    }
}
